﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InformationSystem.BLL.Core.Dao;
using InformationSystem.BLL.Core.Model;
using InformationSystem.Utility;

namespace InformationSystem.BLL.Core.Service.Impl
{

    public class UserServiceImpl : IUserService
    {
        public IUserDao UserDao { get; set; }
        public ICollegeDao CollegeDao { get; set; }

        public LoginUser Login(string userid, string loginpwd)
        {
            //加密源密码
            string secloginpwd = MSecurity.Encrypt(loginpwd);
            userid = userid.ToLower();
            string stu = userid.TrimStart(new char[] { 'j', 'z' });
            //登陆认证
            LoginUser user = this.UserDao.Login(userid, stu, secloginpwd);
            if (user != null)
            {
                if (user.LoginUserType == UserType.Student)
                {
                    user.StudentID = user.UserID;
                    user.ParentID = Common.ParentPre + user.UserID;
                }
                else if (user.LoginUserType == UserType.Parent)
                {
                    user.StudentID = user.UserID.TrimStart(new char[] { 'j', 'z' });
                    user.ParentID = user.UserID;
                }
            }
            //返回结果
            return user;
        }

        public void LogOut(LoginUser user)
        {
            this.UserDao.LogOut(user);
        }


        public void UpdateLoginInfo(LoginUser user, SysEnvironment envir)
        {
            this.UserDao.UpdateLoginInfo(user, envir);
        }

        public List<LoginUser> GetSysUser()
        {
            return this.UserDao.GetSysUser();
        }

        public bool ChangePassword(LoginUser user, string oldpwd, string newpwd)
        {
            //加密源密码
            string myoldpwd = MSecurity.Encrypt(oldpwd);
            string mynewpwd = MSecurity.Encrypt(newpwd);

            if (user.LoginUserType == UserType.Admin || user.LoginUserType == UserType.Assistant)
                return UserDao.ChangeSysUserPassword(user.UserID, myoldpwd, mynewpwd);
            else
                return UserDao.ChangeStudentPassword(user, myoldpwd, mynewpwd);
        }


        public bool EditSysUserInfo(LoginUser user)
        {
            IList<College> colleges = CollegeDao.GetAllCollegeInfo();
            foreach (var item in colleges)
            {
                if (item.CollegeName.Trim() == user.CollegeName.Trim())
                {
                    user.CollegeID = item.CollegeId;
                    break;
                }
            }
            if (!UserDao.UpdateSysUserInfo(user))
            {
                //没有更新到数据，则为新加的数据，插入
                user.LoginPwd = MSecurity.Encrypt(user.UserID);
                return UserDao.CreateSysUserInfo(user);
            }
            return true;
        }


        public bool UpdateSysUserInfo(LoginUser user)
        {
            IList<College> colleges = CollegeDao.GetAllCollegeInfo();
            foreach (var item in colleges)
            {
                if (item.CollegeName.Trim() == user.CollegeName.Trim())
                {
                    user.CollegeID = item.CollegeId;
                    break;
                }
            }
            return UserDao.UpdateSysUserInfo(user);
        }

        public bool CreateSysUserInfo(LoginUser user)
        {
            IList<College> colleges = CollegeDao.GetAllCollegeInfo();
            foreach (var item in colleges)
            {
                if (item.CollegeName.Trim() == user.CollegeName.Trim())
                {
                    user.CollegeID = item.CollegeId;
                    break;
                }
            }
            user.LoginPwd = MSecurity.Encrypt(user.UserID);
            //user.FoldName = Guid.NewGuid().ToString();
            return UserDao.CreateSysUserInfo(user);
        }


        public bool DeleteSysUserByID(string userID)
        {
            return this.UserDao.DeleteSysUserByID(userID);
        }




        public System.Data.DataTable GetUserContact()
        {
            return UserDao.GetUserContact();
        }


        public IList<string> GetCollegeAssistant(LoginUser user)
        {
            return UserDao.GetCollegeAssistant(user);
        }

        public int GetAllStudentCount()
        {
            return UserDao.GetAllStudentCount();
        }

        public int GetStudentCountInGrade(LoginUser user)
        {
            return UserDao.GetStudentCountInGrade(user);
        }

        public int GetAllAssistantCount()
        {
            return UserDao.GetAllAssistantCount();
        }


        public IList<SearchResult> GetUserContactWithFilter(string filter)
        {
            return UserDao.GetUserContactWithFilter(filter);
        }
    }
}
